package com.bxd.baodan.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.bxd.baodan.R;
import com.bxd.baodan.adapter.CommonAdapter;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.item.FamDesignBean.ValueBean;
import com.bxd.baodan.util.StringUtil;
import com.bxd.baodan.widget.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */

public class StudyFragment extends BaseStepFragment {

    private MyGridView gv;
    private View topView;
    private View view;

    private List<ValueBean> studyList = new ArrayList<>() ;

    private int temp = -1;
    CommonAdapter<ValueBean> commonAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.step_7_study, container, false);
        }
        ViewGroup viewGroup = (ViewGroup) view.getParent();
        if (viewGroup != null) {
            viewGroup.removeAllViewsInLayout();
        }
        initView();
        return view;
    }

    private void initView() {





        gv = (MyGridView) view.findViewById(R.id.gv);
        topView = view.findViewById(R.id.title);

        // FIXME: 17/7/14 解决listView跳动问题,将焦点设置到顶部view就行了
        topView.setFocusable(true);
        topView.setFocusableInTouchMode(true);
        topView.requestFocus();

        studyList.clear();
studyList.addAll(getBean().data.condition.education_plan.value);

        commonAdapter = new CommonAdapter<ValueBean>(getActivity(), studyList, R.layout.step_3_item) {
            @Override
            public void convert(ViewHolder holder, final ValueBean item) {

                if (!StringUtil.isEmpty(getDesignStep().education_plan)) {
                    String a = getDesignStep().education_plan;
                    if (item.id.equals(a)) {
                        ((CheckBox) holder.getView(R.id.check)).setChecked(true);
                    }
                }

                ((CheckBox) holder.getView(R.id.check)).setText(item.name);

                ((CheckBox) holder.getView(R.id.check)).setId(holder.getPosition());

                ((CheckBox) holder.getView(R.id.check)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {

                      getDesignStep().education_plan = item.id;
                            // 这段代码来实现单选功能
                            if (temp != -1) {
                                CheckBox tempButton = (CheckBox) view.findViewById(temp);
                                if (tempButton != null) {
                                    tempButton.setChecked(false);
                                }
                            }
                            //得到当前的position
                            temp = buttonView.getId();
                        } else {
                            temp = -1;
                        }
                    }
                });
            }
        };


        gv.setAdapter(commonAdapter);


    }

}
